package com.zhxq.ai.controller;

import com.zhxq.ai.repository.ChatHistoryRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

@RestController
@RequestMapping("/ai/knowledge")
@Tag(name = "知识库调试")
public class knowledgeChatController {

    @Resource
    private ChatHistoryRepository inMemoryChatHistoryRepository;

    @Resource
    private ChatClient simplePdfChatClient;

    /**
     * 知识库聊天
     * @param prompt
     * @param chatId
     * @return
     */
    @PostMapping(value = "/chat", produces = "text/html;charset=UTF-8")
    @Operation(summary = "知识库聊天")
    public Flux<String> chat(String prompt, String chatId) {
        inMemoryChatHistoryRepository.save("knowledge", chatId);
        return simplePdfChatClient
                .prompt()
                .user(prompt)
                .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .stream()
                .content();
    }
}
